package model;

import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

import android.os.Looper;
import android.util.Log;

public class ScoresList {
	private static final String PATH_SCORE_WORLD = "http://www.sacre-willy.com/touche-touche/";
	private static ScoresList _scoresListInstance;

	public static ScoresList getInstance() {
		if (_scoresListInstance == null) {
			_scoresListInstance = new ScoresList();
		}
		return _scoresListInstance;
	}

	private String _stringScoreListWorld;
	private String _stringScoreListLocal;

	private String _stringRank;

	private ScoresList() {
		_stringScoreListLocal = "Aucun score local";
	}

	public void addScore(final int score, final String nickname,
			final String phone_id, final String os) {
		Thread t = new Thread() {
			public void run() {
				Looper.prepare();
				DefaultHttpClient client = new DefaultHttpClient();
				HttpConnectionParams.setConnectionTimeout(client.getParams(),
						15000);
				try {
					String url = PATH_SCORE_WORLD + "?action=add&score="
							+ score + "&nickname="
							+ URLEncoder.encode(nickname) + "&phone_id="
							+ URLEncoder.encode(phone_id)
							+ "&os=Android&version=" + URLEncoder.encode(os)
							+ "&model=&device_name=";

					Log.e("addScore", url);
					HttpGet httpget = new HttpGet(url);
					client.execute(httpget);
				} catch (Exception e) {
				}
				Looper.loop();
			}
		};
		t.start();
	}

	public String getStringRank(final String phoneId) {
		Thread thread = new Thread() {
			public void run() {
				Looper.prepare();
				DefaultHttpClient client = new DefaultHttpClient();
				HttpConnectionParams.setConnectionTimeout(client.getParams(),
						15000);
				HttpResponse response;
				HttpEntity entity;
				try {
					HttpGet httpget = new HttpGet(PATH_SCORE_WORLD + "?action=rank&phone_id="+phoneId);
					response = client.execute(httpget);
					entity = response.getEntity();
					if (entity != null) {
						_stringRank = EntityUtils.toString(entity)
								.trim();
						entity.consumeContent();
					}
				} catch (Exception e) {
				}
				Looper.loop();
			}
		};
		thread.start();
		return _stringRank;
	}

	public String getStringScoreListLocal() {
		return _stringScoreListLocal;
	}

	public String getStringScoreListWorld() {
		Thread thread = new Thread() {
			public void run() {
				Looper.prepare();
				DefaultHttpClient client = new DefaultHttpClient();
				HttpConnectionParams.setConnectionTimeout(client.getParams(),
						15000);

				HttpResponse response;
				HttpEntity entity;

				try {
					HttpGet httpget = new HttpGet(PATH_SCORE_WORLD);
					response = client.execute(httpget);
					entity = response.getEntity();
					if (entity != null) {
						_stringScoreListWorld = EntityUtils.toString(entity)
								.trim();
						entity.consumeContent();
					}
				} catch (Exception e) {
				}
				Looper.loop();
			}
		};
		thread.start();
		return _stringScoreListWorld;
	}
}
